// 导入express
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const axios = require("axios");
// 创建服务器对象
const app = express();

//2. 注册 body-parser 中间件：自动 帮我们把 浏览器 发送来的 post 数据 封装到 request.body 中
app.use(
  bodyParser.urlencoded({
    extended: false,
  })
);
app.use(bodyParser.json());
app.use(cookieParser());

const cors = require("cors");
app.use(cors());
// 注册路由
// app.post('/api/login', (req, res) => {
app.post("/api/login", (req, res) => {
  // const { account, password } = req.body
  const account = req.body.account;
  const password = req.body.password;
  // const  account = req.query.account
  // const  password = req.query.password
  if (account === "admin" && password === "123123") {
    res.cookie("cookieID", "1994", {
      maxAge: 60000 * 60 * 24,
    });
    res.send({
      code: 200,
      msg: "登入成功",
    });
  } else {
    res.send({
      code: 401,
      msg: "账号密码错误，请正确输入",
    });
  }
});
app.get("/api/getMapData", (req, res) => {
  console.log(req.query.code);
  const code = req.query.code || 100000;
  axios
    .get(`https://geo.datav.aliyun.com/areas_v3/bound/${code}_full.json`)
    .then((result) => {
      res.send({
        code: 200,
        data: result.data,
      });
    })
    .catch(err => {
      res.send({
        code: 200,
        data: err,
      });
    })
});

// 开启服务器
app.listen(888, (err) => {
  console.log("success");
});
